<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" :model="model" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="应用名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['appName', validatorRules.appName]" placeholder="请输入应用名称"
                       :disabled="updateDisabled"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="版本号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fileVersion', validatorRules.fileVersion]" placeholder="请输入版本号"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <!-- <a-col :span="12">
            <a-form-item label="服务地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['downloadAddress', validatorRules.downloadAddress]"
                       placeholder="例如:app.tcoret.com" :disabled="updateDisabled"/>
            </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="服务名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['itemName', validatorRules.itemName]" placeholder="例如:tcore"
                       :disabled="updateDisabled"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="安装包名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fileName', validatorRules.fileName]" placeholder="请输入文件名称"
                       :disabled="updateDisabled"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          
          <a-col :span="12" v-if="updateDisabled">
            <a-form-item label="更新" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-radio-group v-model="versionChoose" button-style="solid">
                <a-radio-button :value="1">大版本更新</a-radio-button>
                <a-radio-button :value="2">小版本更新</a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" v-if="versionChoose === 1">
            <a-form-item label="上传苹果包" prop="fileIphone" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload :multiple="false" v-decorator="['fileIphone', validatorRules.fileIphone]" :number=1></j-upload>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="versionChoose === 1">
            <a-form-item label="上传安卓包" prop="fileAndroid" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload :multiple="false" v-decorator="['fileAndroid', validatorRules.fileAndroid]"
                        :number=1></j-upload>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="updateDisabled && versionChoose === 2">
            <a-form-item label="上传更新包" prop="fileUpdate" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload :multiple="false" v-decorator="['fileUpdate', validatorRules.fileUpdate]" :number=1></j-upload>
            </a-form-item>
          </a-col>
        </a-row>
        <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
          <a-button @click="submitForm">提 交</a-button>
        </a-col>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction} from '@/api/manage'
import pick from 'lodash.pick'
import JFormContainer from '@/components/jeecg/JFormContainer'
import {add} from '@/api/app/releaseApp'

export default {
  name: 'MipReleaseAppForm',
  components: {
    JFormContainer,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      colDisabled: false,
      updateDisabled: true,
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        appName: {rules: [{required: true, message: '请输入应用名称!'},]},
        fileVersion: {rules: [{required: true, message: '请输入版本号!'},]},
        downloadAddress: {rules: [{required: true, message: ' 请输入服务地址'},]},
        itemName: {rules: [{required: true, message: '请输入服务地址'},]},
        fileName: {rules: [{required: true, message: '请输入安装包名称!'},]},
        fileAndroid: {rules: [{required: true, message: '请上传安卓安装包!'},]},
        fileIphone: {rules: [{required: true, message: '请上传苹果安装包!'},]},
        fileUpdate: {rules: [{required: true, message: '请上传更新包!'},]},
      },
      url: {
        add: "/app/mipReleaseApp/add",
        edit: "/app/mipReleaseApp/edit",
        queryById: "/app/mipReleaseApp/queryById"
      },
      versionChoose: 1
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        return this.formData.disabled !== false;
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
      //编辑页面禁止修改角色编码
      if (this.model.id) {
        this.colDisabled = false;
        this.updateDisabled = true
      } else {
        this.colDisabled = true;
        this.updateDisabled = false;
      }
      this.form.resetFields();
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'appName', 'fileVersion', 'fileName', 'downloadAddress', 'itemName'))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = {id: this.formData.dataId};
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          //console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    },
  }
}
</script>